package com.cskaoyan.service;

import com.cskaoyan.bean.Config;
import com.cskaoyan.bean.ConfigExample;
import com.cskaoyan.bean.MallConfigVo;
import com.cskaoyan.mapper.ConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    ConfigMapper configMapper;

    @Override
    public Object mall() {
        List<Config> configs = configMapper.selectByExample(new ConfigExample());
        MallConfigVo mallConfig = new MallConfigVo();
        for (Config config : configs) {
            if ("cskaoyan_mall_mall_name".equals(config.getKeyName())){
                mallConfig.setCskaoyanmall_mall_name(config.getKeyValue());
            } else if ("cskaoyan_mall_mall_qq".equals(config.getKeyName())){
                mallConfig.setCskaoyanmall_mall_qq(config.getKeyValue());
            }else if ("cskaoyan_mall_mall_phone".equals(config.getKeyName())){
                mallConfig.setCskaoyanmall_mall_phone(config.getKeyValue());
            }else if ("cskaoyan_mall_mall_address".equals(config.getKeyName())){
                mallConfig.setCskaoyanmall_mall_address(config.getKeyValue());
            }
        }
        return mallConfig;
    }

    @Override
    public Object express() {
        List<Config> configs = configMapper.selectByExample(new ConfigExample());
        Map<String, Object> map = new HashMap<>();
        for (Config config : configs) {
            if ("cskaoyan_mall_express_freight_value".equals(config.getKeyName())) {
                map.put("cskaoyanmall_express_freight_value", config.getKeyValue());
            } else if ("cskaoyan_mall_express_freight_min".equals(config.getKeyName())) {
                map.put("cskaoyanmall_express_freight_min", config.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public Object order() {
        List<Config> configs = configMapper.selectByExample(new ConfigExample());
        Map<String, Object> map = new HashMap<>();
        for (Config config : configs) {
            if ("cskaoyan_mall_order_unconfirm".equals(config.getKeyName())) {
                map.put("cskaoyanmall_order_unconfirm", config.getKeyValue());
            } else if ("cskaoyan_mall_order_unpaid".equals(config.getKeyName())) {
                map.put("cskaoyanmall_order_unpaid", config.getKeyValue());
            } else if ("cskaoyan_mall_order_comment".equals(config.getKeyName())) {
                map.put("cskaoyanmall_order_comment", config.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public Object wx() {
        List<Config> configs = configMapper.selectByExample(new ConfigExample());
        Map<String, Object> map = new HashMap<>();
        for (Config config : configs) {
            if ("cskaoyan_mall_wx_share".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_share", config.getKeyValue());
            } else if ("cskaoyan_mall_wx_index_brand".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_index_brand", config.getKeyValue());
            } else if ("cskaoyan_mall_wx_index_topic".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_index_topic", config.getKeyValue());
            } else if ("cskaoyan_mall_wx_index_hot".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_index_hot", config.getKeyValue());
            } else if ("cskaoyan_mall_wx_catlog_goods".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_catlog_goods", config.getKeyValue());
            } else if ("cskaoyan_mall_wx_catlog_list".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_catlog_list", config.getKeyValue());
            } else if ("cskaoyan_mall_wx_index_new".equals(config.getKeyName())) {
                map.put("cskaoyanmall_wx_index_new", config.getKeyValue());
            }
        }
        return map;
    }

    @Override
    public void mall(HashMap<String,String> map) {
        for(Map.Entry<String, String> entry : map.entrySet()) {
            if ("cskaoyanmall_mall_name".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_mall_name";
                updateSystemSelective(entry,keyName);
            }else if ("cskaoyanmall_mall_qq".equals(entry.getKey())){
                String keyName = "cskaoyan_mall_mall_qq";
                updateSystemSelective(entry,keyName);
            }else if ("cskaoyanmall_mall_phone".equals(entry.getKey())){
                String keyName = "cskaoyan_mall_mall_phone";
                updateSystemSelective(entry,keyName);
            }else if ("cskaoyanmall_mall_address".equals(entry.getKey())){
                String keyName = "cskaoyan_mall_mall_address";
                updateSystemSelective(entry,keyName);
            }
        }
    }

    @Override
    public void express(HashMap<String, String> map) {
        for(Map.Entry<String, String> entry : map.entrySet()) {
            if ("cskaoyanmall_express_freight_min".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_express_freight_min";
                updateSystemSelective(entry,keyName);
            }else if ("cskaoyanmall_express_freight_value".equals(entry.getKey())){
                String keyName = "cskaoyan_mall_express_freight_value";
                updateSystemSelective(entry,keyName);
            }
        }
    }

    @Override
    public void order(HashMap<String, String> map) {
        for(Map.Entry<String, String> entry : map.entrySet()) {
            if ("cskaoyanmall_order_comment".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_order_comment";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_order_unpaid".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_order_unpaid";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_order_unconfirm".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_order_unconfirm";
                updateSystemSelective(entry, keyName);
            }
        }
    }

    @Override
    public void wx(HashMap<String, String> map) {
        for(Map.Entry<String, String> entry : map.entrySet()){
            if ("cskaoyanmall_wx_share".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_share";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_wx_index_brand".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_index_brand";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_wx_index_topic".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_index_topic";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_wx_index_hot".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_index_hot";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_wx_catlog_goods".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_catlog_goods";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_wx_catlog_list".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_catlog_list";
                updateSystemSelective(entry, keyName);
            } else if ("cskaoyanmall_wx_index_new".equals(entry.getKey())) {
                String keyName = "cskaoyan_mall_wx_index_new";
                updateSystemSelective(entry, keyName);
            }
        }
    }

    public void updateSystemSelective(Map.Entry<String, String> entry,String keyName){
        ConfigExample configExample = new ConfigExample();
        ConfigExample.Criteria criteria = configExample.createCriteria();
        criteria.andKeyNameEqualTo(keyName);
        Config config = new Config();
        config.setKeyValue(entry.getValue());
        config.setUpdateTime(new Date());
        int update = configMapper.updateByExampleSelective(config, configExample);
    }
}
